/**
 * The contents of this file are subject to the license and copyright detailed in the LICENSE and NOTICE files at the root of the source tree and available online at
 * http://www.askosi.org/license.html
 */
package be.destin.skos.core;

import java.util.LinkedList;

import javax.xml.bind.annotation.XmlTransient;

import be.destin.rdf.changes.Status;

public class LocalLinkedConcept extends LinkedConcept {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String SESAME_NAMESPACE = "LocalConcept:";

	public LocalLinkedConcept() { // For JAXB...
		
	}

	public LocalLinkedConcept (String anAbout, Status aStatus, String subRel) {
		super(anAbout,aStatus,subRel);
	}
	
	public String getNamespace() {
		return LocalLinkedConcept.SESAME_NAMESPACE;
	}

	public String getScheme_About(ConceptScheme context) {
		return context.getAbout_About(relAbout);
	}
	
	public Concept getConcept(ConceptScheme context) {
		if (relAbout == null || relAbout.isEmpty()) return null;
		return context.getConcept(relAbout);
	}

	public static LinkedList<String> getScheme_About(ConceptScheme context,LinkedList<LocalLinkedConcept> rels) {
		LinkedList<String> result = new LinkedList<String>();
		if (rels == null) return result;
		for (LocalLinkedConcept aRel : rels) {
			if (aRel.getCurrentStatus() == Status.Not_Approved) continue;  // Do not include rejected relations
			result.add(context.getAbout_About(aRel.getAbout()));
		}
		return result;
	}

	public LocalLinkedConcept clean() {
		if (relAbout == null) return null;
		String anAbout = SkosUtil.normalizeAbout(relAbout);
		if (anAbout.isEmpty()) return null;
		String aQualifier = null;
		if (relationQualifier != null && !relationQualifier.isEmpty()) {
			aQualifier = SkosUtil.normalizeAbout(relationQualifier);
		}
		return new LocalLinkedConcept(anAbout,status,aQualifier);
	}
	
	public static LinkedList<String> getAbout(LinkedList<LocalLinkedConcept> rels) {
		LinkedList<String> result = new LinkedList<String>();
		if (rels == null) return result;
		for (LinkedConcept aRel : rels) {
			if (aRel.getCurrentStatus() == Status.Not_Approved) continue;  // Do not include rejected relations
			result.add(aRel.getAbout());
		}
		return result;
	}

	public static LinkedList<LocalLinkedConcept> cleanLocalLinks (LinkedList<LocalLinkedConcept> toBeCleaned) {
		if (toBeCleaned == null) return null;
		LinkedList<LocalLinkedConcept> result = new LinkedList<LocalLinkedConcept>();
		for (LocalLinkedConcept anEntry : toBeCleaned) {
			if (anEntry != null) {
				LocalLinkedConcept cleanEntry = anEntry.clean();
				if (cleanEntry != null) {
					result.add(cleanEntry);
				}
			}
		}
		return result;
	}

	public static void mergeLocalLinks ( LinkedList<LocalLinkedConcept> currRels, LinkedList<LocalLinkedConcept> toBeCleaned) {
		if (toBeCleaned == null) return;
		if (currRels == null) currRels = new LinkedList<LocalLinkedConcept>();
		for (LocalLinkedConcept anEntry : toBeCleaned) {
			if (anEntry != null) {
				LocalLinkedConcept cleanEntry = anEntry.clean();
				if (cleanEntry != null) {
					int pos = currRels.indexOf(cleanEntry);
					if (pos >= 0) {
						LocalLinkedConcept existingRel = currRels.get(pos);
						existingRel.setCurrentStatus(cleanEntry.getCurrentStatus());
						String qual = cleanEntry.getRelationQualifier();
						if (qual != null && !qual.isEmpty()) {
							existingRel.setRelationQualifier(qual);
						}
					} else {
						currRels.add(cleanEntry);
					}
				}
			}
		}
	}
}
